package com.mnkjxy.beauty.view.manager;

import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Observable;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.mnkjxy.beauty.utils.MemoryManager;

/**
 * @author lqp
 *
 */
/**
 * @author lqp
 * 
 */

public class MiddleManage extends Observable {

	private static final String TAG = "middleManage";
	/**
	 * 中间容器
	 */
	private RelativeLayout middle;

	public void setMiddle(RelativeLayout middle) {
		this.middle = middle;
	}

	/**
	 * 中间容器管理对象
	 */
	private static MiddleManage instance;

	private MiddleManage() {
	}

	public static MiddleManage getInstance() {
		if (instance == null) {
			instance = new MiddleManage();
		}
		return instance;
	}

	/**
	 * 类似任务栈用于存放中间视图的历史记录
	 */
	private LinkedList<String> history = new LinkedList<>();

	/**
	 * 用于缓存中间容器的view对象,用内纯换取速度
	 */
	private static Map<String, BasicView> ViewCache;

	static {
		// 内存大小还没修改完毕,最后测试修改
		if (MemoryManager.hasAcailMemory()) {
			ViewCache = new HashMap<>();
		} else {
			ViewCache = new MySoftMap<>();
		}

	}

	/**
	 * 当前展示的View
	 */
	private BasicView currentView;

	/**
	 * 改变中间容器现实的内容
	 * 
	 * @param basicUi
	 *            内容的对象,必须继承basicUI
	 */
	public void changeView(Class<? extends BasicView> targetView) {

		if (history.size() != 0) {
			if (currentView.getClass() == targetView) {
				return;
			}
		}

		BasicView basicView = null;
		String key = targetView.getSimpleName();

		if (ViewCache.containsKey(key)) {

			Log.i(TAG, "重用");
			basicView = ViewCache.get(key);
		} else {

			// 创建,获取构造器,用构造器创建
			try {
				Constructor<? extends BasicView> constructor = targetView.getConstructor(Context.class);
				basicView = constructor.newInstance(getContext());
			} catch (Exception e) {
				e.printStackTrace();
				throw new RuntimeException("在middleNamager,构造发生异常");
			}
			// 缓存视图
			ViewCache.put(key, basicView);
		}
		/*************************** 移除当前显示的视图 ********************************/
		middle.removeAllViews();
		// 清除视图缓存
		if (currentView != null) {
			currentView.onPause();
		}

		View view = basicView.getView();
		// ViewGroup parent = (ViewGroup) view.getParent();
		// if (parent != null) {
		// parent.removeView(view);
		// }
		/*************************** 开始更新需要显示的视图 ********************************/
		// 更新视图数据
		middle.addView(view);
		basicView.onResume();

		// parent = null;// 让内存回收
		// view.startAnimation(android.view.animation.AnimationUtils
		// .loadAnimation(getContext(), R.anim.ia_view_change));
		// 记录当前显示的视图
		currentView = basicView;
		// 记录视图历史
		history.addFirst(key);
		// 视图切换成功,通知另外两个容器改变内容
		setChanged();
		// 将id传递,通知另外两个容器,现在中间容器显示的内容
		notifyObservers(currentView.getId());
		Log.i(TAG, "界面切换成功" + key + "---" + basicView.toString());
	}

	/**
	 * 返回键操作
	 * 
	 * @return
	 */
	public boolean goBack() {
		// TODO Auto-generated method stub
		if (history.size() > 1) {
			history.removeFirst();
			String first = history.getFirst();

			// 移除当前显示的视图
			middle.removeAllViews();
			// 获取需要显示的视图
			currentView.onPause();

			BasicView basicView = ViewCache.get(first);
			basicView.onResume();
			View child = basicView.getView();

			// 移除该视图的父控件
			ViewGroup parent = (ViewGroup) child.getParent();
			if (parent != null) {
				parent.removeView(child);
			}
			// 添加
			middle.addView(child);
			// 记录当前视图
			currentView = ViewCache.get(first);

			// 视图切换成功,通知另外两个容器该表内容
			setChanged();
			// 将id传递,通知另外两个容器,现在中间容器显示的内容
			notifyObservers(currentView.getId());
			return true;
		}
		return false;
	}

	private Context getContext() {
		return middle.getContext();
	}

	/**
	 * 清除所有的返回键历史记录
	 */
	public void deleteHistry() {

		for (int i = 0; i < history.size(); i++) {
			history.remove(i);
		}
	}
}
